package com.home.webapp.base.service.impl;

import com.home.base.core.context.BaseContextHandler;
import com.home.base.core.framework.msg.ServerResponse;
import com.home.common.framework.core.entity.EntityUtils;
import com.home.common.framework.core.service.impl.BaseServiceImpl;
import com.home.common.handler.exception.BizException;
import com.home.common.model.CurrentUser;
import com.home.common.model.TreeNode;
import com.home.webapp.base.entity.BaseRoleMenu;
import com.home.webapp.base.mapper.BasePermsAuthMapper;
import com.home.webapp.base.mapper.BaseRoleMenuMapper;
import com.home.webapp.base.service.BaseRoleMenuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Description: 菜单角色 serviceImpl
 * Created by 王大宸 on 2020-10-05 23:28:38
 * Created with IntelliJ IDEA.
 */
@Service
public class BaseRoleMenuServiceImpl extends BaseServiceImpl<BaseRoleMenuMapper, BaseRoleMenu, String> implements BaseRoleMenuService {
    private static final Logger logger = LoggerFactory.getLogger(BaseRoleMenuServiceImpl.class);
    @Autowired
    private BasePermsAuthMapper permsAuthMapper;

    /***
     * 功能说明：给角色添加菜单
     *
     * @author 王大宸
     * @date 2020/10/7 18:12
     * @param entity entity
     * @return com.home.common.framework.msg.ServerResponse<com.home.webapp.base.entity.BaseRoleMenu>
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseRoleMenu saveInfo(BaseRoleMenu entity) throws BizException {
        try {
            BaseRoleMenu roleMenu = new BaseRoleMenu();
            roleMenu.setRoleId(entity.getRoleId());
            baseMapper.delete(roleMenu);

            String menuIds = entity.getMenuId();
            List<String> menuList = new ArrayList<>(Arrays.asList(menuIds.split(",")));
            if (0 == menuList.size()) {
                return entity;
            }

            BaseRoleMenu one = null;
            List<BaseRoleMenu> list = new ArrayList<>();
            String roleId = entity.getRoleId();
            for (String menuId : menuList) {
                if ("1".equals(menuId)) {
                    continue;
                }
                one = new BaseRoleMenu();
                one.setId(EntityUtils.getId());
                one.setMenuId(menuId);
                one.setRoleId(roleId);
                list.add(one);
            }
            baseMapper.batchSave(list);
            return entity;
        } catch (Exception e) {
            logger.error("给角色添加菜单失败: " + e.toString());
            throw new BizException("给角色添加菜单失败");
        }
    }

    /***
     * 功能说明：获取菜单树
     *
     * @author 王大宸
     * @date 2020/10/7 17:54
     * @param roleId  角色id
     * @return com.home.common.framework.msg.ServerResponse<java.util.List < java.util.Map < java.lang.String, java.lang.Object>>>
     */
    @Override
    public ServerResponse<List<TreeNode>> menuTree(String roleId, CurrentUser user) {
        List<TreeNode> list = new ArrayList<>();
        List<TreeNode> apps = new ArrayList<>();
        if ("admin".equals(user.getUsername())) {
            list = baseMapper.getMenuAdmin();
            apps = baseMapper.getAppAdmin();
        } else {
            // 这里查询权限下发表中的权限
            list = permsAuthMapper.getPermsAuth(BaseContextHandler.getUserId());
            apps = permsAuthMapper.getPermsAuthApp(BaseContextHandler.getUserId());
        }
        list.addAll(apps);

        List<String> roles = baseMapper.getMenuByRole(roleId);
        for (TreeNode treeNode : list) {
            if (roles.contains(treeNode.getId())) {
                treeNode.setChecked(true);
            }
        }
        list.add(new TreeNode("1", "请选择菜单", "0", true, "0"));
        return ServerResponse.okData(list);
    }
}
